package Components
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.events.FlexMouseEvent;
	
	public class EditableTitlePanel extends DraggablePanel
	{
		private var tabPressed:Boolean;
		protected var titleInput:TextInput;
		
		public function get editingTitle():Boolean
		{
			return titleBar.contains(titleInput);
		}
				
		protected override function createChildren():void
		{
			super.createChildren();
			if(!titleInput)
			{
				titleInput = new TextInput();
				titleInput.addEventListener(KeyboardEvent.KEY_DOWN, inputKeyDownHandler);
				titleInput.addEventListener(FocusEvent.FOCUS_OUT, hideHandler);
				titleInput.tabIndex = 0;
			}
			if(titleBar)
			{
				titleBar.doubleClickEnabled = true;
				titleBar.addEventListener(MouseEvent.DOUBLE_CLICK, toggleHandler);
				titleBar.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, hideHandler);
			}
		}
		
		protected override function layoutChrome(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.layoutChrome(unscaledWidth, unscaledHeight);
			if(titleInput)
			{
				titleInput.move(titleTextField.x, 3);
				titleInput.setActualSize(unscaledWidth - titleTextField.x - 10,
										 DEFAULT_MEASURED_HEIGHT);
			}
		}
		
		public function EditTitle(val:Boolean):void
		{
			if(val && editingTitle) return;
			if(!val && !editingTitle) return;
			
			if(val)
			{
				titleInput.text = titleTextField.text;
				titleBar.addChild(titleInput);
				titleInput.selectionBeginIndex = 0;
				titleInput.selectionEndIndex = titleInput.text.length;
				focusManager.setFocus(focusManager.findFocusManagerComponent(titleInput));
			}
			else
			{
				title = titleInput.text;
				titleInput.focusManager.hideFocus();
				titleBar.removeChild(titleInput);
			}
		}
		
		protected function hideHandler(evt:Event):void
		{
			EditTitle(false);
		}
		protected function showHandler(evt:Event):void
		{
			EditTitle(true);
		}
		protected function toggleHandler(evt:Event):void
		{
			EditTitle(!editingTitle);
		}
		
		protected function inputKeyDownHandler(evt:KeyboardEvent):void
		{
			if(evt.keyCode == Keyboard.ESCAPE)
				titleInput.text = titleTextField.text;
			if(evt.keyCode == Keyboard.ENTER || evt.keyCode == Keyboard.ESCAPE)
			{
				evt.stopImmediatePropagation();
				hideHandler(evt);
				titleBar.setFocus();
			}
			if(evt.keyCode == Keyboard.TAB)
			{
				firstChild = getChildAt(0) as UIComponent;
				firstChild.setFocus();
				firstChild.addEventListener(FocusEvent.FOCUS_OUT, focusOutHack);
			}
		}
		
		/**
		 * This is a workaround. We cannot prevent the default behavior of the TAB key,
		 * so this function does a 1-time capture of the focus_out event, and returns
		 * focus to the firstChild, which is where we wanted focus to go
		 */
		private var firstChild:UIComponent;
		private function focusOutHack(evt:Event):void
		{
			firstChild.removeEventListener(FocusEvent.FOCUS_OUT, focusOutHack);
			firstChild.setFocus();
		}
	}
}